package kz.pompei.OneGwt.client.personrole.impl;

import kz.pompei.OneGwt.client.personrole.FormMode;
import kz.pompei.OneGwt.client.personrole.PersonFormView;
import kz.pompei.OneGwt.ifaces.util.ClientUtil;
import kz.pompei.OneGwt.ifaces.util.Transpazon;
import kz.pompei.OneGwt.ifaces.widget.BoolWidget;
import kz.pompei.OneGwt.ifaces.widget.ButtonWidget;
import kz.pompei.OneGwt.ifaces.widget.NullAdapters;
import kz.pompei.OneGwt.ifaces.widget.TextWidget;
import kz.pompei.OneGwt.ifaces.widget.gwtadapters.Button_impl_ButtonWidget;
import kz.pompei.OneGwt.ifaces.widget.gwtadapters.CheckBox_impl_BoolWidget;
import kz.pompei.OneGwt.ifaces.widget.gwtadapters.TextBoxBase_impl_TextWidget;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PersonFormViewImpl implements PersonFormView {
  
  private DialogBox dialogBox;
  private final Transpazon<UIObject> relativeObject = new Transpazon<UIObject>();
  private TextWidget textAccountId = NullAdapters.noTextWidget();
  private TextWidget textName = NullAdapters.noTextWidget();
  private TextWidget textSurname = NullAdapters.noTextWidget();
  private BoolWidget active = NullAdapters.noBoolWidget();
  private ButtonWidget buttonSave = NullAdapters.noButton();
  private ButtonWidget buttonCancel = NullAdapters.noButton();
  
  @Override
  public ButtonWidget buttonCancel() {
    return buttonCancel;
  }
  
  public Transpazon<UIObject> getRelativeObjectTranspazon() {
    return relativeObject;
  }
  
  @Override
  public ButtonWidget buttonSave() {
    return buttonSave;
  }
  
  @Override
  public BoolWidget active() {
    return active;
  }
  
  @Override
  public TextWidget textAccountId() {
    return textAccountId;
  }
  
  @Override
  public TextWidget textName() {
    return textName;
  }
  
  @Override
  public TextWidget textSurname() {
    return textSurname;
  }
  
  @Override
  public void hideForm() {
    getDialogBox().hide();
  }
  
  @Override
  public void showForm() {
    getDialogBox().show();
    
    UIObject target = relativeObject.get();
    if (target != null) {
      ClientUtil.locateByCenter( getDialogBox(), target );
    } else {
      getDialogBox().center();
    }
  }
  
  private DialogBox getDialogBox() {
    if (dialogBox == null) {
      return dialogBox = createDialogBox();
    }
    return dialogBox;
  }
  
  @Override
  public void setFormMode(FormMode mode) {
    getDialogBox().setText(
        mode == FormMode.ADDING ? "Добавление пользователя" :"Изменение пользователя" );
  }
  
  private DialogBox createDialogBox() {
    DialogBox ret = new DialogBox( false );
    ret.setModal( true );
    
    VerticalPanel wall = new VerticalPanel();
    
    wall.setWidth( "20em" );
    ret.setWidget( wall );
    
    {
      Grid grid = new Grid( 4, 2 );
      
      {
        HTML w = new HTML( "Фамилия:" );
        w.addStyleName( "one-TextBoxLabel" );
        grid.setWidget( 0, 0, w );
      }
      
      {
        TextBox txt = new TextBox();
        grid.setWidget( 0, 1, txt );
        
        textSurname = new TextBoxBase_impl_TextWidget( txt );
      }
      {
        HTML w = new HTML( "Имя:" );
        w.addStyleName( "one-TextBoxLabel" );
        grid.setWidget( 1, 0, w );
      }
      {
        TextBox txt = new TextBox();
        grid.setWidget( 1, 1, txt );
        
        textName = new TextBoxBase_impl_TextWidget( txt );
      }
      {
        HTML w = new HTML( "Аккаунт:" );
        w.addStyleName( "one-TextBoxLabel" );
        grid.setWidget( 2, 0, w );
      }
      {
        TextBox txt = new TextBox();
        grid.setWidget( 2, 1, txt );
        
        textAccountId = new TextBoxBase_impl_TextWidget( txt );
      }
      {
        CheckBox checkBox = new CheckBox();
        checkBox.setText( "Активность" );
        grid.setWidget( 3, 1, checkBox );
        
        active = new CheckBox_impl_BoolWidget( checkBox );
      }
      wall.add( grid );
    }
    
    {
      HorizontalPanel forButtons = new HorizontalPanel();
      forButtons.setSpacing( 5 );
      {
        Button b = new Button( "Сохранить" );
        forButtons.add( b );
        
        buttonSave = new Button_impl_ButtonWidget( b );
      }
      {
        Button b = new Button( "Отменить" );
        forButtons.add( b );
        
        buttonCancel = new Button_impl_ButtonWidget( b );
      }
      wall.add( forButtons );
    }
    
    return ret;
  }
  
}
